<template>
  <el-dialog
    :title="!dataForm.productno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="长度" prop="length">
        <el-input v-model="dataForm.length" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input v-model="dataForm.width" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="厚度" prop="thickness">
        <el-input v-model="dataForm.thickness" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input v-model="dataForm.weight" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="materialtypegroup">
        <el-input v-model="dataForm.materialtypegroup" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        productno: 0,
        length: '',
        width: '',
        thickness: '',
        weight: '',
        materialtypegroup: ''
      },
      dataRule: {
        length: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        width: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        thickness: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        materialtypegroup: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.productno = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.productno) {
          this.$http({
            url: this.$http.adornUrl(`/materialcopy/materialtestcopy1/info/${this.dataForm.productno}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.length = data.materialTestCopy1.length
              this.dataForm.width = data.materialTestCopy1.width
              this.dataForm.thickness = data.materialTestCopy1.thickness
              this.dataForm.weight = data.materialTestCopy1.weight
              this.dataForm.materialtypegroup = data.materialTestCopy1.materialtypegroup
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/materialcopy/materialtestcopy1/${!this.dataForm.productno ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'productno': this.dataForm.productno || undefined,
              'length': this.dataForm.length,
              'width': this.dataForm.width,
              'thickness': this.dataForm.thickness,
              'weight': this.dataForm.weight,
              'materialtypegroup': this.dataForm.materialtypegroup
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
